package shop.maytinh.view;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

import shop.maytinh.model.Comment;
import shop.maytinh.model.Product;
import shop.maytinh.controller.CommentManager;
import shop.maytinh.controller.ProductManager;
import shop.maytinh.controller.CatalogiesManager;
import shop.maytinh.controller.FactoryManager;




public class Products extends AbstractClass{
	private Integer idProduct;
	private Integer id;
	private String messenger;
	Product product=null;	
	List<Comment> lstcomment ;
	Comment comment;
	private String aaa;
	ProductManager productmanager = new ProductManager();
	CommentManager commentmanager = new CommentManager();
	List<Product> listproduct ;
	public String execute() {
		try{
			
			product = productmanager.getProductById(id);
			if(product!=null)
			{		
			request.getSession().setAttribute("Product", product);
			}
			
			lstcomment = commentmanager.getCommentByProduct(id);
			
			if(member!=null)
			{
				aaa=member.getFullname();
			}
			 return "success";
			}
			catch(Exception e){
				return "error";
			}
	}	
	
	public static String convertDateToString(Date date, String format) {
	    SimpleDateFormat mySimpleDateFormat = new SimpleDateFormat(format);
	    return mySimpleDateFormat.format(date);
	}
	public String addcmt()
	{
		try{
			if(member!=null)
			{
				//product = productmanager.getProductById(1);
				Product p = productmanager.getProductById(idProduct);
				comment.setMember(member);
				comment.setProduct(p);
				
				String format1 = "dd/MM/yyyy";
				Date d = new Date();
				String date1 = convertDateToString(d,format1);
				
				DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
				Date myDate = formatter.parse(date1);
				java.sql.Date sqlDate = new java.sql.Date(myDate.getTime());
				
				comment.setDate(sqlDate);
				comment.setCreated_at(sqlDate);
				comment.setUpdated_at(sqlDate);	
				commentmanager.add(getComment());
				
				return "success";
			}
			else 
			{
				messenger = "Bạn Phải Đăng Nhập Trước Khi Đăng Comment...!";
				return "fail";
			}
		
		}
		catch(Exception e)
		{
			return "error";
		}
	}
	
	public String listProductByCatalogy()
	{
	try{
		CatalogiesManager cmm = new CatalogiesManager();
		listproduct = cmm.getProductByIdCatalogy(id);
		return "success";
		}
		catch(Exception e)
		{
			return "error";
		}
		
	}
	public String listProductByFactory()
	{
	try{
		FactoryManager factorymn =  new FactoryManager();				 
		listproduct = factorymn.getProductByIdFactory(id);
		return "success";
		}
		catch(Exception e)
		{
			return "error";
		}
		
	}
	public String buy()
	{
		return "success";
	}
	
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	public String getAaa() {
		return aaa;
	}
	public void setAaa(String aaa) {
		this.aaa = aaa;
	}
	public List<Comment> getLstcomment() {
		return lstcomment;
	}
	public void setLstcomment(List<Comment> lstcomment) {
		this.lstcomment = lstcomment;
	}

	public Comment getComment() {
		return comment;
	}

	public void setComment(Comment comment) {
		this.comment = comment;
	}

	public Integer getIdProduct() {
		return idProduct;
	}

	public void setIdProduct(Integer idProduct) {
		this.idProduct = idProduct;
	}

	public String getMessenger() {
		return messenger;
	}

	public void setMessenger(String messenger) {
		this.messenger = messenger;
	}

	public List<Product> getListproduct() {
		return listproduct;
	}

	public void setListproduct(List<Product> listproduct) {
		this.listproduct = listproduct;
	}		
	
	
}

